/*******************************************************************************
 * Copyright 2011 Andrea Guitto
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.andreaguitto.mobile.android.framework.base.orm;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public abstract class AGSQLiteDatabaseSession extends AGSQLiteOpenHelper {

    public AGSQLiteDatabaseSession(AGSQLiteAppDatabaseMetadata metadata, Context context) {
		super(metadata, context);
	}

	private AGSQLiteOpenHelper DatabaseOpenHelper;
    private SQLiteDatabase databaseObject;
    
    //---opens the database---
    public synchronized final AGSQLiteDatabaseSession open(boolean writeable) throws SQLException 
    {
 	   	if(databaseObject==null || !databaseObject.isOpen()){
 	   		if(writeable)
 	   			databaseObject = this.getWritableDatabase();
 	   		else
 	   			databaseObject = this.getReadableDatabase();
 	   		
          	databaseObject.setLockingEnabled(false);
	   	}
	    
    	return this;
    }
    
    public final void beginTrasaction(){
    	boolean isReady = false;
    	try{
    		isReady = onBeginTransaction();
    	}catch(Exception e){
    		e.printStackTrace();
    		isReady = false;
    	}
    	
    	if(isReady){
        	Log.d(getLogTag(), "Begin Database Transiction");
        	if(databaseObject.inTransaction()){
        		rollbackTrasaction();
        		Log.w(getLogTag(), "Another database transaction was in pending, rollbacked it!");
        	}
    		databaseObject.beginTransaction();
    	}
    }

    public final void commitTransaction(){
    	
    	boolean isReady = false;
    	try{
    		isReady = onCommitTransaction();
    	}catch(Exception e){
    		e.printStackTrace();
    		isReady = false;
    	}
    	
    	if(isReady){
        	Log.d(getLogTag(), "Commit Database Transiction");
        	if(databaseObject.inTransaction()){
		    	databaseObject.setTransactionSuccessful();
		    	databaseObject.endTransaction();
        	}
    	}
    }
    
    public final void rollbackTrasaction(){
    	
    	boolean isReady = false;
    	try{
    		isReady = onRollbackTransaction();
    	}catch(Exception e){
    		e.printStackTrace();
    		isReady = false;
    	}
    	
    	if(isReady){
	    	Log.d(getLogTag(), "Rollback Database Transiction");
	    	if(databaseObject.inTransaction())
	    		databaseObject.endTransaction();
    	}
    }
    
    //---closes the database---    
    public final void closeAndRelease() 
    {
    	
    	boolean isReady = false;
    	try{
    		isReady = onCloseAndRelease();
    	}catch(Exception e){
    		e.printStackTrace();
    		isReady = false;
    	}
    	
    	if(isReady){
	    	if(DatabaseOpenHelper!=null){
	    		DatabaseOpenHelper.close();
	    	}
	    	
	    	SQLiteDatabase.releaseMemory();
    	}
    }
    
    public synchronized final SQLiteDatabase getDatabase() {	return databaseObject;	}

    public abstract String getLogTag();
    
    public abstract boolean onCloseAndRelease();
    
    public boolean onBeginTransaction(){	return true;	}
    public boolean onCommitTransaction(){	return true;	}
    public boolean onRollbackTransaction(){		return true;	}
}
